﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLVS
{
    
    public partial class Page : UserControl
    {
        int MaxMineNumber = 10;                                   //雷数上限
        int lm;                                                   //剩余雷数
        int MapWidth = 9;                                        //地图宽度
        int MapHeight = 9;                                       //地图长度
        static MineClass[,] m = new MineClass[30,16];             //地雷数组
        bool MakeMap = false;                                     //是否制作按钮
        bool IsSearching = true;                                  //是在搜索状态还是标记状态
        bool StartGame = false;
        int TagSum = 0;                                           //全部标记数
        int TagMinedSum = 0;                                      //命中数
        int level = 1;                                            //难度
     

        public Page()
        {
            InitializeComponent();

            win.Visibility = Visibility.Collapsed;
            

            this.MouseLeftButtonDown += new MouseButtonEventHandler(PageMouseDown);

        }

        //Events...

        void PageMouseDown(object sender, RoutedEventArgs e)
        {

            if (StartGame)
            {
                if (TagMinedSum == MaxMineNumber && TagSum == TagMinedSum)
                    WinGame();
                for (int i = 0; i < MapWidth; i++)
                {
                    for (int j = 0; j < MapHeight; j++)
                    {
                        if (m[i, j].clicking)
                        {

                            m[i, j].clicking = false;

                            if (m[i, j].tagged)
                            {
                                m[i, j].tagged = false;
                                m[i, j].Retrieve();
                                continue;
                            }
                            FindMine(i, j);
                        }

                    }
                }
            }
            
                
        }

        //Functions....

        public void InitializeMap()//初始化地图
        {
            RemoveMap();
            ForePanel.Visibility = Visibility.Collapsed;
            switch (level)
            {
                case 1: MapWidth = 9; MapHeight = 9; MaxMineNumber = 10; break;
                case 2: MapWidth = 16; MapHeight = 16; MaxMineNumber = 50; break;
                case 3: MapWidth = 30; MapHeight = 16; MaxMineNumber = 99; break;

            }
            for (int i = 0; i < MapWidth; i++)
            {
                for (int j = 0; j < MapHeight; j++)
                {
                    m[i, j] = new MineClass();
                    m[i, j].Left = (i + 1) * 25 + 15;
                    m[i, j].Top = (j + 1) * 25 + 80;
                    m[i, j].X = i;
                    m[i, j].Y = j;
                    LayoutRoot.Children.Add(m[i, j]);
                }
            }//创建地雷
            MakeMap = true;
            win.Visibility = Visibility.Collapsed;
            lm = MaxMineNumber;
            LeftMineNo.Text = lm.ToString();
            for (int i = 0; i < MapWidth; i++)
            {
                for (int j = 0; j < MapHeight; j++)
                {
                    m[i, j].clicked = false;
                    m[i, j].mined = false;
                    m[i, j].mnum = 0;
                    m[i, j].tagged = false;

                }
            }
            Random r = new Random();
            for (int i = 0; i < MaxMineNumber; i++)//随机布雷
            {
                int x, y;
                x = r.Next(0, MapWidth);
                y = r.Next(0, MapHeight);
                if (m[x, y].mined)
                {
                    i--;
                    continue;
                }
                else
                {
                    m[x, y].mined = true;
                    //m[x, y].btn.Content = "M";//测试用的
                }
            }
        }

        void RemoveMap()
        {
            if (MakeMap)
            {
                for (int i = 0; i < MapWidth; i++)
                {
                    for (int j = 0; j < MapHeight; j++)
                    {
                        LayoutRoot.Children.Remove(m[i, j]);
                    }
                }
            }
        }

        void FindMine(int X, int Y)
        {
            if (IsSearching)//搜索状态
            {
                if (m[X, Y].mined)//误点雷了……挂了……
                    LoseGame();
                else//探测中……
                {

                    for (int i = -1; i <= 1; i++)//如果m[i,j]周围有雷
                    {
                        for (int j = -1; j <= 1; j++)
                        {
                            if (!CheckMine(X + i, Y + j))
                            {
                                continue;
                            }
                            if (m[X + i, Y + j].mined && !m[X, Y].clicked)
                            {
                                m[X, Y].mnum++;
                                m[X, Y].btn.Content = m[X, Y].mnum;
                            }
                        }
                    }

                    m[X, Y].IsClicked();//先把中间的点了再说……

                    if (m[X, Y].mnum == 0)//如果没有雷，则向四周展开搜索
                    {
                        for (int i = -1; i <= 1; i++)
                        {
                            for (int j = -1; j <= 1; j++)
                            {
                                if (!CheckMine(X + i, Y + j) || (i == 0 && j == 0))
                                {
                                    continue;
                                }
                                if (!m[X + i, Y + j].clicked)
                                {
                                    FindMine(X + i, Y + j);
                                }
                            }
                        }
                    }
                }
            }
            else//标记状态中……
            {
                if (!m[X, Y].clicked)
                {
                    if (lm>0)
                        lm--;
                    LeftMineNo.Text = lm.ToString();
                    m[X, Y].btn.Content = "X";
                    m[X, Y].tagged = true;//标记变量为真
                    m[X, Y].IsClicked();
                    TagSum++;
                    if (m[X,Y].mined)
                        TagMinedSum++;
                }
            }
        }

        bool CheckMine(int x, int y)//处理边界问题
        {
            if (x < 0 || y < 0 || x > MapWidth-1 || y > MapHeight-1)
                return false;
            else
                return true;
        }

        void WinGame()
        {
            win.Text = "You Win!";
            win.Visibility = Visibility.Visible;
        }

        void LoseGame()
        {
            win.Text = "You Lose...";
            win.Visibility = Visibility.Visible;
            StartGame = false;
            for (int i = 0; i < MapWidth; i++)
            {
                for (int j = 0; j < MapHeight; j++)
                {
                    m[i, j].IsClicked();
                    if (m[i, j].mined)
                        m[i, j].btn.Content = "M";
                }
            }
        }

        private void Restart_Click(object sender, RoutedEventArgs e)
        {
            RemoveMap();
            ForePanel.Visibility = Visibility.Visible;
        }

        private void Searching_Click(object sender, RoutedEventArgs e)
        {
            Button s = (Button)sender;
            if (IsSearching)
            {
                s.Content = "Find!";
                IsSearching = false;
            }
            else
            {
                s.Content = "Searching...";
                IsSearching = true;
            }

        }

        private void EasyLevel_Checked(object sender, RoutedEventArgs e)
        {
            level = 1;
        }

        private void NormalLevel_Checked(object sender, RoutedEventArgs e)
        {
            level = 2;
        }

        private void HardLevel_Checked(object sender, RoutedEventArgs e)
        {
            level = 3;
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            InitializeMap();
            StartGame = true;
            ForePanel.Visibility = Visibility.Collapsed;
        }


    }
}
